package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.LocalPlatformTypeAttrHandler;
import com.iwhalecloud.bss.kite.dataservice.cache.DcPublicCache;
import com.iwhalecloud.bss.kite.dataservice.entity.DcPublic;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.scene.attr.OperateInstAttrService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 *
 */
@Service
@AttrHandler(LocalPlatformTypeAttrHandler.class)
public class LocalPlatformTypeAttrLinkHandler extends AbstractAttrLinkageHandler<AttrData>  {

    @Resource
    private OperateInstAttrService operateInstAttrService;

    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
        KiteAttr platSettlePriceAttr = inst.getAttr("200004099");
        KiteAttr platSellPriceAttr = inst.getAttr("200004100");
        DcPublic dcPublic = DcPublicCache.get("201230001", "PRICE_CONFIG");
        if (Objects.isNull(dcPublic)) {
            return;
        }
        if ("100006800".equals(attr.getValue())) {
            if (!Objects.isNull(platSettlePriceAttr)) {
                platSettlePriceAttr.setDefaultValue(dcPublic.getCodea());
                operateInstAttrService.setAttrValue(inst, "200004099", dcPublic.getCodea(), data.getSceneInstId(), null);

            }
            if (!Objects.isNull(platSellPriceAttr)) {
                platSellPriceAttr.setDefaultValue(dcPublic.getCodeb());
                operateInstAttrService.setAttrValue(inst, "200004100", dcPublic.getCodeb(), data.getSceneInstId(), null);
            }
        } else if ("100006801".equals(attr.getValue())) {
            if (!Objects.isNull(platSettlePriceAttr)) {
                platSettlePriceAttr.setDefaultValue( dcPublic.getCodec());
                operateInstAttrService.setAttrValue(inst, "200004099", dcPublic.getCodec(), data.getSceneInstId(), null);
            }
            if (!Objects.isNull(platSellPriceAttr)) {
                platSellPriceAttr.setDefaultValue(dcPublic.getCoded());
                operateInstAttrService.setAttrValue(inst, "200004100", dcPublic.getCoded(), data.getSceneInstId(), null);
            }
        }
    }

    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {

    }
}
